package ihm;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import metier.Salle;
import dao.DaoProjetCinema;

public class FenetreAjoutSalle extends JInternalFrame implements ActionListener
{
	private JTextField txtNbrePlaces;
	private JLabel lblNombreDePlaces;
	private JButton btnAjouter;
	private Vector<Salle> lesSalles;

	/**
	 * Create the frame.
	 */
	public FenetreAjoutSalle(Vector <Salle> lesSalles)
	{
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		
		this.txtNbrePlaces = new JTextField();
		this.txtNbrePlaces.setBounds(154, 86, 86, 20);
		getContentPane().add(txtNbrePlaces);
		this.txtNbrePlaces.setColumns(10);
		
		this.lblNombreDePlaces = new JLabel("Nombre de places : ");
		this.lblNombreDePlaces.setBounds(21, 89, 123, 14);
		getContentPane().add(lblNombreDePlaces);
		
		this.btnAjouter = new JButton("Ajouter");
		this.btnAjouter.addActionListener(this);
		this.btnAjouter.setBounds(319, 218, 91, 23);
		getContentPane().add(btnAjouter);

		this.lesSalles = lesSalles;
	}
	
	public void actionPerformed(ActionEvent evt) 
	{
		Salle laSalle;
		int nb;
		
		if (evt.getSource() == this.btnAjouter)
		{
			if (this.txtNbrePlaces.getText().isEmpty())
				JOptionPane.showMessageDialog(null, "Tous les champs sont obligatoires !");
			else
			{
				laSalle = new Salle(Integer.parseInt(this.txtNbrePlaces.getText()));
				nb = DaoProjetCinema.ajouterSalle(laSalle);
				if (nb == 0)
					JOptionPane.showMessageDialog(null, "Cette salle existe déjà !");
				else
				{
					JOptionPane.showMessageDialog(null, "Salle ajoutée.");
					this.lesSalles.add(laSalle);
				}
			}	
		}
	}
}
